#pragma once

#include <iostream>
#include <string>
#include <ctime>
#include <pthread.h>
#include <stdint.h>

typedef void (*callback_t)();
static int num = 1;

class Thread
{
public:
    Thread(callback_t cb)
        :_tid(0)
        ,_name("")
        ,_start_timestamp(0)
        ,_isrunning(false)
        ,_cb(cb)
    {}
    void Run()
    {
        _name = "thread-" + std::to_string(num++);
        _start_timestamp = time(nullptr);
        _isrunning = true;
        pthread_create(&_tid,nullptr,Routine,this);
    }
    // 线程等待
    void Join()
    {
        pthread_join(_tid,nullptr);
        _isrunning = false;
    }
    // 获取线程名
    std::string Name()
    {
        return _name;
    }
    // 获取线程开始时间
    uint64_t StartTimestamp()
    {
        return _start_timestamp;
    }
    // 线程是否运行
    bool IsRunning()
    {
        return _isrunning;
    }
    // 执行传入的函数
    void Entery()
    {
        _cb();
    }
    ~Thread()
    {}
    
private:
    static void *Routine(void *args)
    {
        Thread *thread = static_cast<Thread*>(args);
        thread->Entery();

        return nullptr;
    }
private:
    pthread_t _tid;
    std::string _name;
    uint64_t _start_timestamp;
    bool _isrunning;

    callback_t _cb;
};